const CompressionWebpackPlugin = require('compression-webpack-plugin')
const isProd = process.env.NODE_ENV === 'production'
const productionGzipExtensions = ['js', 'css', 'scss']
const baseURL = require('./src/ipconfig')
const port = process.env.port || process.env.npm_config_port || 9527

const proxyList = {}
const name = '宏宇集团汽车订单系统'
if (!isProd) {
  Object.keys(baseURL.proxy).forEach(index => {
    proxyList[`/${index}`] = {
      target: baseURL.proxy[index],
      changeOrigin: true, // 是否跨域
      pathRewrite: {
        [`^/${index}`]: '' // 重写接口
      }
    }
  })
}
const Ver = isProd && userVer()
module.exports = {
  publicPath: process.env.VUE_APP_ENV === 'production' ? 'https://cdn.j6375x.cn/cdn/ds/' + Ver : '.',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  // pages: pages,
  devServer: {
    port: port,
    open: true,
    // openPage: '/system1.html',
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: proxyList
  },
  transpileDependencies: [
    'vue-echarts',
    'resize-detector'
  ],
  configureWebpack: config => {
    config.devtool = 'source-map'
    config.name = name
    if (isProd) {
      config.resolve.extensions = ['.js', '.json', '.vue', '.scss', '.css']
      config.plugins.push(
        new CompressionWebpackPlugin({
          // asset: '[path].gz[query]',
          algorithm: 'gzip',
          test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
          threshold: 1024,
          cache: true,
          minRatio: 0.8
        })
      )
    }
  },
  css: {
    loaderOptions: {
      // 给 sass-loader 传递选项
      sass: {
        // @/ 是 src/ 的别名
        // 所以这里假设你有 `src/variables.scss` 这个文件
        data: `@import "@/styles/mixin.scss";`
      }
    }
  },
  productionSourceMap: true
}
//  生成打包的目录

function userVer() {
  return new Date().getTime()
}
